package org.zfes.snowier.zbss.xzy.biz.solution.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;
import org.zfes.snowier.zbss.xzy.biz.solution.dao.SolutionDeviceConfigValueMapper;
import org.zfes.snowier.zbss.xzy.biz.solution.model.AlgorithmConfigItem;
import org.zfes.snowier.zbss.xzy.biz.solution.model.SolutionDeviceConfigValue;
import org.zfes.snowier.zbss.xzy.biz.solution.service.IAlgorithmConfigItemService;
import org.zfes.snowier.zbss.xzy.biz.solution.service.ISolutionDeviceConfigValueService;
import org.zfes.snowier.zbss.xzy.solution.ConfigValueUpdateDto;
import org.zfes.snowier.zbss.xzy.solution.SolutionDeviceConfigValueDto;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class SolutionDeviceConfigValueServiceImpl implements ISolutionDeviceConfigValueService{
 	@Autowired
	private SolutionDeviceConfigValueMapper algorithmConfigItemValueMapper;
	
	@Autowired
	private  IAlgorithmConfigItemService algorithmConfigItemService;
 	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAlgorithmConfigItemValue(SolutionDeviceConfigValue algorithmConfigItemValue) {
		ZBeanUtil.setCreateInfo(algorithmConfigItemValue);
		ZBeanUtil.validateBean(algorithmConfigItemValue);
		algorithmConfigItemValueMapper.insertSelective(algorithmConfigItemValue);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SolutionDeviceConfigValue> loadAlgorithmConfigItemValueById(Long id) {
		if(id!=null){
			return Optional.ofNullable(algorithmConfigItemValueMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAlgorithmConfigItemValue(SolutionDeviceConfigValue algorithmConfigItemValue) {
		ZBeanUtil.setUpdatedInfo(algorithmConfigItemValue);
		ZBeanUtil.validateBean(algorithmConfigItemValue);
		algorithmConfigItemValueMapper.updateByPrimaryKeySelective(algorithmConfigItemValue);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteAlgorithmConfigItemValue(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		algorithmConfigItemValueMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setAlgorithmConfigItemValueStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		algorithmConfigItemValueMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAlgorithmConfigItemValueDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(algorithmConfigItemValueMapper.selectListCount(pm), algorithmConfigItemValueMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SolutionDeviceConfigValueDto> loadDeviceConfigedValue(Long deviceId, Long solutionId, Long algorithmId) {
		List<AlgorithmConfigItem>  algConfigItemList= algorithmConfigItemService.loadByAlgorithmId(algorithmId, Byte.valueOf("1"));
		
		List<SolutionDeviceConfigValue> configItemValueList = algorithmConfigItemValueMapper
				.selectDeviceSolutionConfigedValue( deviceId,  solutionId,  algorithmId);
		
		return algConfigItemList.stream().map(item->{
			SolutionDeviceConfigValueDto acvDto=new SolutionDeviceConfigValueDto();
			ZBeanUtil.copy(item, acvDto);
			acvDto.setId(item.getId());
			acvDto.setParentId(item.getParentId());
			acvDto.setDeviceId(deviceId);
			acvDto.setAlgorithmId(algorithmId);
			acvDto.setSolutionId(solutionId);
			configItemValueList.stream().filter(v->
										deviceId.equals(v.getDeviceId()) && 
										solutionId.equals(v.getSolutionId())&&
										algorithmId.equals(v.getAlgorithmId())&&
										item.getId().equals(v.getItemId()))
								.findAny().ifPresent(vv->{
									acvDto.setCofvalue(vv.getCofvalue());
									acvDto.setCofremark(vv.getRemark());
								});;
			
			return acvDto;
		}).collect(Collectors.toList());
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SolutionDeviceConfigValue> loadConfigValue(Long deviceId, Long solutionId, Long algorithmId, Long itemId){
		if(deviceId==null||solutionId==null||algorithmId==null||itemId==null) {
			return Optional.empty();
		}
		
		return Optional.ofNullable(algorithmConfigItemValueMapper.selectConfigValue(deviceId,solutionId,algorithmId,itemId));
		
	}
	@Override
	public void updateConfigValue(ConfigValueUpdateDto configValueUpdate) {
		if(configValueUpdate.getDeviceId()==null||
				configValueUpdate.getSolutionId()==null||
				configValueUpdate.getAlgorithmId()==null||
				configValueUpdate.getItemId()==null||
				ZStrUtil.hasNoText(configValueUpdate.getCofvalue())) {
			ZAlert.throwSLE("参数错误");
		}
		Optional<SolutionDeviceConfigValue> val=loadConfigValue(
				configValueUpdate.getDeviceId(), 
				configValueUpdate.getSolutionId(),
				configValueUpdate.getAlgorithmId(), 
				configValueUpdate.getItemId());
		if(val.isPresent()) {	
			algorithmConfigItemValueMapper.updateConfigValue(
					configValueUpdate.getDeviceId(), 
					configValueUpdate.getSolutionId(),
					configValueUpdate.getAlgorithmId(), 
					configValueUpdate.getItemId(), 
					configValueUpdate.getCofvalue());
		}else {
			createAlgorithmConfigItemValue(new SolutionDeviceConfigValue(
					configValueUpdate.getDeviceId(), 
					configValueUpdate.getSolutionId(),
					configValueUpdate.getAlgorithmId(), 
					configValueUpdate.getItemId(), 
					configValueUpdate.getCofvalue()) );
		}
		
	}
    
}